import Vue from 'vue'
import VueRouter from 'vue-router'
import cookie from "js-cookie";
import {Message} from 'element-ui'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Navigation',
    component: ()=> import("../components/Home"),
    redirect: '/index',  //路由重定向为/index路径
    children: [
      //首页
      {
        path: '/index',
        name: 'index',
        component: () => import('@/view/index') //实现组件切换
      },
      {
        path: '/login',
        name: 'login',
        component: () => import('@/view/login')
      },
      {
        path: '/register',
        name: 'register',
        component: () => import('@/view/register')
      },
      {
        path: '/PlaneList',
        name: 'PlaneList',
        component: () => import('@/view/PlaneList')
      }, {
        path: '/OrderList',
        name: 'OrderList',
        component: () => import('@/view/OrderList')
      }, , {
        path: '/MyOrder',
        name: 'MyOrder',
        component: () => import('@/view/MyOrder')
      },
      {
        path: '/ToPay/:orderNo',
        name: 'ToPay',
        component: () => import('@/view/ToPay')
      },
      {
        path: '/404',
        name: '404页面',
        component: () => import('@/view/Page404')

      }


    ]

  },
  {//重定向至 404
    path: '*',
    redirect: '/404',
    hidden: true
  }
]
// 全局前置路由守卫，表示在每次切换路由之前调用，针对所有的路由
// to表示去哪个路由，from表示来自哪个路由，next表示放行

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, from, next) => {
  // 可加判断条件进行放行
  if (to.path === '/login' || to.path === '/index' || to.path === '/404'|| to.path === '/register') {    // 判断要去的路由条件
    next()      // 放行
  } else {
    if (cookie.get('token') == null) {
      next('/login')
      Message({
        message: '请先登录！',
        type: 'error',
        duration: 5 * 1000
      })
    } else {
      next()
    }


  }
})


/**
 * export主要用于对外输出本模块变量的接口，一个文件就可以被理解为一个模块。export就是导出。
 */
export default router

